home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility3 / wtj008.zip / NOTBAD.ZIP / OPENFILE.CPP < prev    next >
C/C++ Source or Header  |  1992-06-15  |  6KB  |  182 lines

  1. // openfile.cpp RHS 3/31/91
  2.  
  3. #include"openfile.h"
  4.  
  5. char    OpenFileDlg::filename[FILENAME_MAX];
  6. char    OpenFileDlg::openname[FILENAME_MAX];
  7. char    OpenFileDlg::defpath[FILENAME_MAX];
  8. char    OpenFileDlg::defspec[13];
  9. char    OpenFileDlg::default_extension[4];
  10. char    OpenFileDlg::str[FILENAME_MAX*2];
  11. char    OpenFileDlg::caption[20];
  12. char    OpenFileDlg::buttontext[20];
  13. char    OpenFileDlg::mode;
  14. WORD    OpenFileDlg::attribute;
  15.  
  16. unsigned  OpenFileDlg::hEditBox;
  17. unsigned  OpenFileDlg::hListBox;
  18. unsigned  OpenFileDlg::hPath;
  19.  
  20. // lets user select and open files
  21. BOOL CALLBACK OpenFileDlg::DlgProc(HWND hDlg, UINT message, 
  22.     WPARAM wParam, LPARAM lParam)
  23.     {
  24.     BOOL result;
  25.  
  26.     switch (message)
  27.         {
  28.         case WM_INITDIALOG:                        // message: initialize    
  29.             SetDlgItemText(hDlg,IDOK,buttontext);
  30.             SetWindowText(hDlg,caption);
  31.             UpdateListBox(hDlg);
  32.             SetDlgItemText(hDlg, hEditBox, defspec);
  33.             SendDlgItemMessage(hDlg,hEditBox,EM_SETSEL,NULL,
  34.                 MAKELONG(0, 0x7fff));
  35.             SetFocus(GetDlgItem(hDlg, hEditBox));
  36.             return FALSE; // Indicates the focus is set to a control
  37.  
  38.         case WM_COMMAND:
  39.             if(wParam == hListBox)
  40.                 {
  41.                 switch (HIWORD(lParam))
  42.                     {
  43.                     case LBN_SELCHANGE:
  44.                         //  If item is a directory name, append "*.*" 
  45.                         if(DlgDirSelect(hDlg, str, hListBox))
  46.                             strcat(str, defspec);
  47.  
  48.                         SetDlgItemText(hDlg, hEditBox, str);
  49.                         SendDlgItemMessage(hDlg,hEditBox,EM_SETSEL,NULL,MAKELONG(0, 0x7fff));
  50.                         break;
  51.  
  52.                     case LBN_DBLCLK:
  53.                         goto openfile;
  54.                     }
  55.                 return TRUE;
  56.                 }
  57.  
  58.             switch (wParam)
  59.                 {
  60.                 case IDOK:
  61. openfile:
  62.                     GetDlgItemText(hDlg, hEditBox, openname, FILENAME_MAX);
  63.                     if(strchr(openname, '*') || strchr(openname, '?'))
  64.                         {
  65.                         SeparateFilePath((LPSTR) str, (LPSTR) defspec,
  66.                             (LPSTR) openname);
  67.                         if(str[0])
  68.                             strcpy(defpath, str);
  69.                         ChangeDfExt(default_extension, defspec);
  70.                         UpdateListBox(hDlg);
  71.                         return TRUE;
  72.                         }
  73.  
  74.                     if(!openname[0])
  75.                         {
  76.                         MessageBox(hDlg, "No filename specified.",
  77.                             NULL, MB_OK | MB_ICONHAND);
  78.                         return TRUE;
  79.                         }
  80.  
  81.                     AddExtension(openname, default_extension);
  82.  
  83.                     if(mode == OPENFILE_FILEOPEN)
  84.                         {
  85.                         OFSTRUCT OfStruct;
  86.                         int hdl;
  87.  
  88.                         if((hdl = OpenFile(openname,&OfStruct,OF_EXIST)) == -1)
  89.                             {
  90.                             filename[0] = '\0';
  91.                             result = FALSE;
  92.                             }
  93.                         else
  94.                             {
  95.                             result = TRUE;
  96.                             strcpy(filename, openname);
  97.                             }
  98.                         _lclose(hdl);
  99.                         }
  100.                     else    // mode is FILESAVE
  101.                         {
  102.                         result = TRUE;
  103.                         strcpy(filename,openname);
  104.                         }
  105.  
  106.                     EndDialog(hDlg, result);
  107.                     return TRUE;
  108.  
  109.                 case IDCANCEL:
  110.                     EndDialog(hDlg, FALSE);
  111.                     return TRUE;
  112.                 }
  113.         }
  114.     return FALSE;
  115.     }
  116.  
  117. // updates the list box in the Open Dialog
  118. void OpenFileDlg::UpdateListBox(HWND hDlg)
  119.     {
  120.     strcpy(str, defpath);
  121.     strcat(str, defspec);
  122.     DlgDirList(hDlg, str, hListBox, hPath, 0x4010);
  123.  
  124.         // list is for subdir of current drive directory
  125.     if(!strchr (defpath, ':'))
  126.         DlgDirList(hDlg, defspec, hListBox, hPath, 0x4010);
  127.  
  128.         // remove the '..', if one
  129.     if(strstr (defpath, ".."))
  130.         defpath[0] = '\0';
  131.     SetDlgItemText(hDlg, hEditBox, defspec);
  132.     }
  133.  
  134. // changes default extension
  135. void OpenFileDlg::ChangeDfExt(char *Ext, char *Name)
  136.     {
  137.     char *pTptr;
  138.  
  139.     pTptr = Name;
  140.     while(*pTptr && *pTptr != '.')
  141.         pTptr++;
  142.     if(*pTptr)
  143.         if(!strchr(pTptr, '*') && !strchr(pTptr, '?'))
  144.             strcpy(Ext, pTptr);
  145.     }
  146.  
  147. // separates file and path names
  148. void OpenFileDlg::SeparateFilePath(LPSTR lpDestPath,
  149.     LPSTR lpDestFileName, LPSTR lpSrcFileName)
  150.     {
  151.     LPSTR lpTmp;
  152.     char  cTmp;
  153.  
  154.     lpTmp = lpSrcFileName + (long) lstrlen(lpSrcFileName);
  155.     while(*lpTmp != ':' && *lpTmp != '\\' && lpTmp > lpSrcFileName)
  156.         lpTmp = AnsiPrev(lpSrcFileName, lpTmp);
  157.     if(*lpTmp != ':' && *lpTmp != '\\') {
  158.         lstrcpy(lpDestFileName, lpSrcFileName);
  159.         lpDestPath[0] = 0;
  160.         return;
  161.     }
  162.     lstrcpy(lpDestFileName, lpTmp + 1);
  163.     cTmp = *(lpTmp + 1);
  164.     lstrcpy(lpDestPath, lpSrcFileName);
  165.     *(lpTmp + 1) = cTmp;
  166.     lpDestPath[(lpTmp - lpSrcFileName) + 1] = 0;
  167.     }
  168.  
  169. // adds default extension
  170. void OpenFileDlg::AddExtension(char *Name, char *Ext)
  171.     {
  172.     char *pTptr;
  173.  
  174.     pTptr = Name;
  175.     while(*pTptr && *pTptr != '.')
  176.         pTptr++;
  177.     if(*pTptr != '.')
  178.         strcat(Name, Ext);
  179.     }
  180.  
  181.  
  182.